/*
 * @Author: C.
 * @Date: 2021-12-22 11:14:14
 * @LastEditTime: 2023-12-07 10:53:44
 * @Description: file content
 */
// 日期格式化
export const dateFm = (date: any, fmt = 'yyyy-MM-dd') => {
  // fmt=
  if (!date) return '--'
  const currentDate = new Date(date)
  const o = {
    'M+': currentDate.getMonth() + 1, // 月份
    'd+': currentDate.getDate(), // 日
    'h+': currentDate.getHours(), // 小时
    'm+': currentDate.getMinutes(), // 分
    's+': currentDate.getSeconds(), // 秒
    'q+': Math.floor((currentDate.getMonth() + 3) / 3), // 季度
    S: currentDate.getMilliseconds(), // 毫秒
  }
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      `${currentDate.getFullYear()}`.slice(4 - RegExp.$1.length)
    )
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.slice(`${o[k]}`.length)
      )
  }

  return fmt
}
export const timeFm = (date: any, fmt = 'yyyy-MM-dd hh:mm:ss') => {
  return dateFm(date, fmt)
}

export function fromTime(time: any, option = null) {
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - Number(d)) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return `${Math.ceil(diff / 60)}分钟前`
  } else if (diff < 3600 * 24) {
    return `${Math.ceil(diff / 3600)}小时前`
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return timeFm(time, option)
  } else {
    return timeFm(time)
  }
}
/**
 * 获取某个时间范围
 *
 * @param v -1、-7、-14、-30、-60
 * @returns object {start: y-m-d,end: y-m-d}
 */
export function getRangeDate(v: string) {
  const now = new Date()
  const nowTime = now.getTime()
  const oneDay = 24 * 60 * 60 * 1000
  const dateRange = { start: '', end: '' }
  const nowWeekDay = now.getDay() // 今天本周的第几天
  const nowDay = now.getDate() // 当前日
  const nowMonth = now.getMonth() // 当前月
  const nowYear = now.getFullYear() // 当前年

  /**
   * 获得某个月的天数
   * @param month 当前月份
   */
  const getMonthDays = function (month: any) {
    const monthStartDate = new Date(nowYear, month, 1)
    const monthEndDate = new Date(nowYear, month + 1, 1)
    const days = (monthEndDate - monthStartDate) / oneDay
    return days
  }

  // 昨日
  if (v === '-1') {
    dateRange.start = dateFm(new Date(nowTime - oneDay), 'yyyy-MM-dd')
    dateRange.end = dateRange.start
    // 本周
  } else if (v === '-7') {
    const weekStart = new Date(nowYear, nowMonth, nowDay - nowWeekDay + 1)
    const weekEnd = new Date(nowTime + oneDay) // 今日
    dateRange.start = dateFm(weekStart, 'yyyy-MM-dd')
    dateRange.end = dateFm(weekEnd, 'yyyy-MM-dd')
    // 上周
  } else if (v === '-14') {
    const weekStart = new Date(nowYear, nowMonth, nowDay - nowWeekDay - 6)
    const weekEnd = new Date(nowYear, nowMonth, nowDay - nowWeekDay)
    dateRange.start = dateFm(weekStart, 'yyyy-MM-dd')
    dateRange.end = dateFm(weekEnd, 'yyyy-MM-dd')
    // 本月
  } else if (v === '-30') {
    const monthStart = new Date(nowYear, nowMonth, 1)
    const monthEnd = new Date(nowTime + oneDay)
    dateRange.start = dateFm(monthStart, 'yyyy-MM-dd')
    dateRange.end = dateFm(monthEnd, 'yyyy-MM-dd')
    // 上月
  } else if (v === '-60') {
    const lastMonthDate = new Date() // 上月日期
    lastMonthDate.setDate(1)
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
    const lastMonth = lastMonthDate.getMonth()
    const lastMonthStart = new Date(nowYear, lastMonth, 1)
    const lastMonthEnd = new Date(nowYear, lastMonth, getMonthDays(lastMonth))
    dateRange.start = dateFm(lastMonthStart, 'yyyy-MM-dd')
    dateRange.end = dateFm(lastMonthEnd, 'yyyy-MM-dd')
  } else {
    // 传入 v 为整数是即为近 xx 天
    if (v > 0) {
      dateRange.start = dateFm(
        new Date(nowTime - oneDay * Number.parseInt(v)),
        'yyyy-MM-dd'
      )
      dateRange.end = dateFm(new Date(nowTime - oneDay), 'yyyy-MM-dd') // 不含今天
    }
  }
  return dateRange
}
